#coding=UTF-8
'''
#重要经营指标
@author: joseph
'''
import datetime,os
from constant.Constant import *;
from xlrd import open_workbook
from xlutils.save import save
from xlwt.Workbook import Workbook

#old='zcfzb';#TODO
report={1:u"一季报",2:u"半年报",3:u"三季报",4:u"年报"};
url_sec='zyjyzb'#TODO
suffix='.html'
year=datetime.date.today().year;
month=def_month;
selected_month="";
sheets={}
#stocks=[300001];# for test

url = concatUrl(url_head,concatUrl(url_sec, url_sec))

#add paras to the url
def para(code,year,month=def_month):
#    max_year=datetime.date.today().year;
    if(year==datetime.date.today().year and (month==def_month or month==selected_month)):
        return paraCurrentYear(code)
    return "?stockCode="+str(code)+"&yyyy="+str(year)+"&mm="+str(month);#ignore "max_yyyy="+max_year

def paraCurrentYear(code):
    return "?stockCode="+str(code);

#return False if there are no data found        
def addRow(book,row=0,code=min_stock_code,type='116'):
    u=urlUncomplete+str(code)+suffix;#add parameters in year
    soup = soupUrl(u);
    dataForm = soup.find('table',{'cellspacing':'2','cellpadding':'3'})
    col=0;
    for k in sheets.keys():
        if('116'==type):
            sheets[k].write(row,col,code);
        else:
            sheets[k].write(row,col,'股票代码');
    if(dataForm==None):
        return False;
    datas=dataForm.findAll('td',{'width':'118'});#use find to get the first result TODO
    for n in range(1,len(datas)):#remove the first one
        col=col+1;
        d=datas[n];
        if('118'==type):
            for k in sheets.keys():
                sheets[k].write(row,col,d.text);            
        if('116'==type):
            title=datas[0].nextSibling.nextSibling;
            d=datas[n].nextSibling.nextSibling;
            if(None==title or ""==title):
                continue
#            print("adding "+title.text)
            key=title.text.strip().encode(encode);
            if(sheets.has_key(key)):
                sheet=sheets[key]
                sheet.write(row,col,str(d.text)); 
            while(None!=title and ""!=title.text):
                title=title.nextSibling.nextSibling;
                d=d.nextSibling.nextSibling;
                if(None==title or ""==title.text):
                    continue
#                print("adding "+title.text)
                key=title.text.strip().encode(encode);
                if(sheets.has_key(key)):
                    sheet=sheets[key]
                    sheet.write(row,col,str(d.text));             
    return True;

def addSheet(book):
    if(False==addRow(book,0,min_stock_code,'118')):#add title'重要指标'
        return False;
    row=1;
    for code in stocks:
        addRow(book, row, code);#add data
        row=row+1;
    return True;

dir_name=u"重要经营指标"
if(False==os.path.isdir(dir_name)):
    os.mkdir(dir_name)    
#财务指标
currentYear=datetime.date.today().year;
for u in report.keys():
    book=Workbook(encode);
    urlUncomplete=url+str(u);#TODO
    sheets={}
    for y in range(2006,currentYear+1):
        #year=y#the addRow use the year para
        name=str(y)+report[u];
        print(name);
        newSheet=book.add_sheet(name);
        sheets[name.encode(encode)]=newSheet
    addSheet(book)
    n=dir_name+u"/"+report[u]+u".xls";
    print(n)
    book.save(n);

#重要经营指标
